﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ServerTools.Tools;
namespace ServerTools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = Application.StartupPath;
            ReadData();
            ShowData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            SetPathTextBox(folderBrowserDialog1.SelectedPath);
            textBox1.Text = folderBrowserDialog1.SelectedPath;
            DataChange();
        }

        private void SetPathTextBox(string path)
        {
            if (CheckFolderPath(path))
            {
                textBox1.BackColor = Color.LightGreen;
                InitMapListView(textBox1.Text + "\\server");
                textBox5.Text = File.ReadAllText(textBox1.Text + "\\run.bat");
                textBox5.Text = textBox5.Text.Replace("\n", "\r\n");

                textBox5.Text = "cd " + path + "\r\n" + textBox5.Text;
            }
            else
                textBox1.BackColor = Color.Red;
        }

        private bool CheckFolderPath(string path)
        {
            if (Directory.Exists(path) == false)
                return false;

            if (Directory.Exists(path + "\\client") == false)
                return false;

            if (Directory.Exists(path + "\\client_AI") == false)
                return false;

            if (Directory.Exists(path + "\\server") == false)
                return false;

            if (Directory.Exists(path + "\\ui") == false)
                return false;

            if (File.Exists(path + "\\run.bat") == false)
                return false;

            return true;
        }

        private void ReadData()
        {
            if (File.Exists("C:\\ai_tank_config.ini"))
            {
                INI ini = new INI("C:\\ai_tank_config.ini");

                textBox1.Text = ini.Read("ServerCongfig", "path");
                textBox2.Text = ini.Read("ServerCongfig", "ip");
                textBox3.Text = ini.Read("ServerCongfig", "port");

                trackBar1.Value = StringToInt32(ini.Read("ServerCongfig", "round"));
                trackBar2.Value = StringToInt32(ini.Read("ServerCongfig", "reg"));
                trackBar3.Value = StringToInt32(ini.Read("ServerCongfig", "action"));
                trackBar4.Value = StringToInt32(ini.Read("ServerCongfig", "team"));
                trackBar5.Value = StringToInt32(ini.Read("ServerCongfig", "tank"));
                trackBar6.Value = StringToInt32(ini.Read("ServerCongfig", "life"));
                trackBar7.Value = StringToInt32(ini.Read("ServerCongfig", "delay"));
            }
            SetPathTextBox(textBox1.Text);
        }

        private int StringToInt32(string str)
        {
            if (str.Length != 0)
                return Convert.ToInt32(str);
            return 0;
        }

        private void InitMapListView(string path)
        {
            listView1.Items.Clear();
            if (Directory.Exists(path) == false)
                return;

            string[] maps = Directory.GetFiles(path, "map_*.txt");
            int tempHeight, tempWidth;
            for (int i = 0; i < maps.Length; i++)
            {
                listView1.Items.Add(new ListViewItem((i + 1).ToString()));
                listView1.Items[i].SubItems.Add(Path.GetFileNameWithoutExtension(maps[i]));

                ReadMapFileWidthHeight(maps[i], out tempHeight, out tempWidth);
                listView1.Items[i].SubItems.Add(Path.GetFileNameWithoutExtension(tempWidth.ToString()));
                listView1.Items[i].SubItems.Add(Path.GetFileNameWithoutExtension(tempHeight.ToString()));
            }
            if (listView1.Items.Count > 0)
            {
                listView1.Items[0].Selected = true;
                label19.Text = listView1.Items[0].SubItems[1].Text;
            }

            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void ReadMapFileWidthHeight(string path, out int height, out int width)
        {
            if (File.Exists(path) == false)
            {
                height = 0;
                width = 0;
            }
            string map = File.ReadAllText(path);
            string[] separator = new string[] { "\r\n" };
            string[] rows = map.Split(separator, StringSplitOptions.None);
            if (rows.Length < 2)
            {
                height = 0;
                width = 0;
            }
            else
            {
                width = Convert.ToInt32(rows[0]);
                height = Convert.ToInt32(rows[1]);
            }
        }

        private void SaveData()
        {
            if (File.Exists("C:\\ai_tank_config.ini") == false)
                File.Create("C:\\ai_tank_config.ini");

            INI ini = new INI("C:\\ai_tank_config.ini");
            ini.Write("ServerCongfig", "path", textBox1.Text);
            ini.Write("ServerCongfig", "ip", textBox2.Text);
            ini.Write("ServerCongfig", "port", textBox3.Text);
            ini.Write("ServerCongfig", "round", trackBar1.Value.ToString());
            ini.Write("ServerCongfig", "reg", trackBar2.Value.ToString());
            ini.Write("ServerCongfig", "action", trackBar3.Value.ToString());
            ini.Write("ServerCongfig", "team", trackBar4.Value.ToString());
            ini.Write("ServerCongfig", "tank", trackBar5.Value.ToString());
            ini.Write("ServerCongfig", "life", trackBar6.Value.ToString());
            ini.Write("ServerCongfig", "delay", trackBar7.Value.ToString());

            if (listView1.SelectedItems.Count != 0)
            {
                ini.Write("ServerCongfig", "map", listView1.Items[listView1.SelectedItems[0].Index].SubItems[1].Text);
            }
        }

        #region 滚动条位置被改变
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label5.Text = trackBar1.Value.ToString() + "回合";
            DataChange();
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            label7.Text = trackBar2.Value.ToString() + "秒";
            DataChange();
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            label9.Text = trackBar3.Value.ToString() + "毫秒";
            DataChange();
        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            label11.Text = trackBar4.Value.ToString() + "队";
            DataChange();
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            label13.Text = trackBar5.Value.ToString() + "辆";
            DataChange();
        }

        private void trackBar6_Scroll(object sender, EventArgs e)
        {
            label15.Text = trackBar6.Value.ToString() + "次";
            DataChange();
        }

        private void trackBar7_Scroll(object sender, EventArgs e)
        {
            label17.Text = trackBar7.Value.ToString() + "毫秒";
            DataChange();
        }
        #endregion

        private void DataChange()
        {
            ShowData();
            timer1.Stop();
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SaveData();
            timer1.Stop();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            DataChange();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            DataChange();
        }

        private void ShowData()
        {
            label5.Text = trackBar1.Value.ToString() + "回合";
            label7.Text = trackBar2.Value.ToString() + "秒";
            label9.Text = trackBar3.Value.ToString() + "毫秒";
            label11.Text = trackBar4.Value.ToString() + "队";
            label13.Text = trackBar5.Value.ToString() + "辆";
            label15.Text = trackBar6.Value.ToString() + "次";
            label17.Text = trackBar7.Value.ToString() + "毫秒";
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否恢复默认值？", "询问", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            textBox2.Text = "172.0.0.1";
            textBox3.Text = "6000";

            trackBar1.Value = 150;
            trackBar2.Value = 30;
            trackBar3.Value = 400;
            trackBar4.Value = 1;
            trackBar5.Value = 4;
            trackBar6.Value = 4;
            trackBar7.Value = 0;
            SaveData();
            ShowData();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                label19.Text = listView1.SelectedItems[0].SubItems[1].Text;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string path = textBox1.Text + "\\server\\" + label19.Text + ".txt";
            if (File.Exists(path))
                Process.Start(path);
        }

        private Process batProcess;

        private void button5_Click(object sender, EventArgs e)
        {
            string path = textBox1.Text + "\\run.bat";
            string path1 = textBox1.Text + "\\temp.bat";
            if (File.Exists(path) == false)
                return;
            File.Copy(path, path1, true);

            File.WriteAllText(path1, "cd " + textBox1.Text + "\nrun.bat");
            Process.Start(path1);
        }

        private void BatProcess_Exited(object sender, EventArgs e)
        {
            MessageBox.Show("fsdf");
        }

        private void ServerProcess_Exited(object sender, EventArgs e)
        {
            MessageBox.Show("游戏线程已退出！");
        }

        private void SetCommandLine()
        {
            string path = textBox1.Text;



        }

        private void button6_Click(object sender, EventArgs e)
        {
            ProcessCtrl.CloseProcess("nw");
        }
    }
}
